exports.MMElevatorAttributeWindow = GL2.Node.subclass(
{
    classname: 'MMElevatorAttributeWindow',
    
    initialize: function(elevatorContainer) 
    {
		this._backgroundImage = null;
		this._closeButton = null;
		this._elevatorName = null;
		this._elevatorModel = null;
		this._elevatorContainer = null;
		this._removeElevatorButton = null;
		this._thumbImage = null;
		this._elevatorDescription = null;
		
		this._elevatorContainer = elevatorContainer;
		this._elevatorModel = elevatorContainer.getModelObject();
    },
	
	createView: function() 
	{
		this.makeTouchSwallower();
		
		var backgroundImagePath = this.getBaseResourcePath() + "background.png";
		
		var closeButtonImageNormal = this.getBaseResourcePath() + "close-button-normal.png";
		var closeButtonImagePressed = this.getBaseResourcePath() + "close-button-pressed.png";
		
		var genericButtonNormalImagePath = this.getBaseResourcePath() + "generic-button-normal.png";
		var genericButtonSelectedImagePath = this.getBaseResourcePath() + "generic-button-pressed.png";
		
		var fillerBarHolderImagePath = this.getBaseResourcePath() + "filler-bar-holder.png";
		var fillerBarImagePath = this.getBaseResourcePath() + "filler-bar.png";
		
		var instantRestockImagePathNormal = this.getBaseResourcePath() + "instant-finish-restock-normal.png";
		var instantRestockImagePathPressed = this.getBaseResourcePath() + "instant-finish-restock-pressed.png";
		
		var instantUpgradeImagePathNormal = this.getBaseResourcePath() + "instant-finish-upgrade-normal.png";
		var instantUpgradeImagePathPressed = this.getBaseResourcePath() + "instant-finish-upgrade-pressed.png";
			
		this._backgroundImage = GLFactory.getSprite([TCUtil.getAdjustedX(0),TCUtil.getAdjustedY(45)], null, backgroundImagePath, [TCUtil.getAdjustedX(320),TCUtil.getAdjustedY(380)]);
		this.addChild(this._backgroundImage);
		
		this._elevatorName = GLFactory.getText([TCUtil.getAdjustedX(110), TCUtil.getAdjustedY(30)], 100, this._elevatorModel.getName(), [TCUtil.getAdjustedX(100), TCUtil.getAdjustedY(50)], TCUtil.getAdjustedFontSize(120), null, null, GL2.Text.OverflowMode.ReduceFontSize, [0.0, 0.5], new Core.Color(0,0,0));
		this._backgroundImage.addChild(this._elevatorName);
		
		
		
		if(TutorialManager.getHasCompletedTutorial())
		{ 
			this._closeButton = new GLButton(closeButtonImageNormal, closeButtonImagePressed, TCUtil.getAdjustedX(40),TCUtil.getAdjustedY(40), this.onCloseTouch.bind(this), this._backgroundImage, 3, "", null, null, null,null);
			this._closeButton.setPosition(TCUtil.getAdjustedX(280), TCUtil.getAdjustedY(25));
			
			//this._removeElevatorButton = new GLButton(genericButtonNormalImagePath, genericButtonSelectedImagePath, TCUtil.getAdjustedX(100),TCUtil.getAdjustedY(40), this.onCloseTouch.bind(this), this._backgroundImage, 3, Core.Localization.getString("REMOVE_ELEVATOR"),new Core.Color(0,0,0), null, null, TCUtil.getAdjustedFontSize(90));
			//this._removeElevatorButton.setPosition(TCUtil.getAdjustedX(70), TCUtil.getAdjustedY(140));
		
		}
		else
		{
			if(TutorialManager.getCurrentTutorialStep() == 9)
			{
				this._closeButton = new GLButton(closeButtonImageNormal, closeButtonImagePressed, TCUtil.getAdjustedX(40),TCUtil.getAdjustedY(40), this.onCloseTouch.bind(this), this._backgroundImage, 3, "", null, null, null,null);
				this._closeButton.setPosition(TCUtil.getAdjustedX(280), TCUtil.getAdjustedY(25));
			}			
		}
				
//		this._closeButton = new GLButton(closeButtonImageNormal, closeButtonImagePressed, TCUtil.getAdjustedX(40),TCUtil.getAdjustedY(40), this.onCloseTouch.bind(this), this._backgroundImage, 3, "", null, null, null,null);
//		this._closeButton.setPosition(TCUtil.getAdjustedX(280), TCUtil.getAdjustedY(25));

		this._thumbImage = GLFactory.getSprite([TCUtil.getAdjustedX(45),TCUtil.getAdjustedY(40)], null, this._elevatorContainer.getThumbImagePath(), [TCUtil.getAdjustedX(45),TCUtil.getAdjustedY(80)]);
		this._backgroundImage.addChild(this._thumbImage);	
		
		this._elevatorDescription = GLFactory.getText([TCUtil.getAdjustedX(127), TCUtil.getAdjustedY(80)], 100, Core.Localization.getString("ELEVATOR_DESCRIPTION"), [TCUtil.getAdjustedX(170), TCUtil.getAdjustedY(100)], TCUtil.getAdjustedFontSize(120), null, null, GL2.Text.OverflowMode.Multiline, [0.0, 0.5], new Core.Color(0,0,0));
		this._backgroundImage.addChild(this._elevatorDescription);		
		
//		this._stockLabel = GLFactory.getText([TCUtil.getAdjustedX(55), TCUtil.getAdjustedY(220)], 100,"Stock 0% full", [TCUtil.getAdjustedX(100), TCUtil.getAdjustedY(50)], TCUtil.getAdjustedFontSize(120), null, null, GL2.Text.OverflowMode.ReduceFontSize, [0.0, 0.5], new Core.Color(0,0,0));
//		this._backgroundImage.addChild(this._stockLabel);
		
//		this._restockButton = new GLButton(genericButtonNormalImagePath, genericButtonSelectedImagePath, TCUtil.getAdjustedX(100),TCUtil.getAdjustedY(40), this.onCloseTouch.bind(this), this._backgroundImage, 3, "Restock",new Core.Color(0,0,0), null, null, TCUtil.getAdjustedFontSize(90));
//		this._restockButton.setPosition(TCUtil.getAdjustedX(215), TCUtil.getAdjustedY(220));
				
		this.drawRequiredUI();
		
//		this._stockLabel = GLFactory.getText([TCUtil.getAdjustedX(18), TCUtil.getAdjustedY(180)], 100,"Stock 0% full.", [TCUtil.getAdjustedX(100), TCUtil.getAdjustedY(50)], TCUtil.getAdjustedFontSize(100), null, null, GL2.Text.OverflowMode.ReduceFontSize, [0.0, 0.5], new Core.Color(0,0,0));
//		this._backgroundImage.addChild(this._stockLabel);
//		
//		this._restockFillerBarHolder = GLFactory.getSprite([TCUtil.getAdjustedX(120),TCUtil.getAdjustedY(155)], null, fillerBarHolderImagePath, [TCUtil.getAdjustedX(170),TCUtil.getAdjustedY(50)]);
//		this._backgroundImage.addChild(this._restockFillerBarHolder);				
//		
//		this._restockingFillerBar = GLFactory.getSprite([TCUtil.getAdjustedX(11),TCUtil.getAdjustedY(4)], null, fillerBarImagePath, [TCUtil.getAdjustedX(100),TCUtil.getAdjustedY(37)]);///x=159 for full
//		this._restockFillerBarHolder.addChild(this._restockingFillerBar);
//		
//		this._restockingLabel = GLFactory.getText([TCUtil.getAdjustedX(11), TCUtil.getAdjustedY(4)], 100,"Restocking", [TCUtil.getAdjustedX(150), TCUtil.getAdjustedY(40)], TCUtil.getAdjustedFontSize(130), null, null, GL2.Text.OverflowMode.ReduceFontSize, [0.0, 0.0], new Core.Color(0,0,0));
//		this._restockFillerBarHolder.addChild(this._restockingLabel);
//		
//		this._instantRestockingButton = new GLButton(instantRestockImagePathNormal, instantRestockImagePathPressed, TCUtil.getAdjustedX(260),TCUtil.getAdjustedY(40), this.onCloseTouch.bind(this), this._backgroundImage, 3, "Instantly finish restocking now for 3 nuggets!",new Core.Color(0,0,0), null, null, TCUtil.getAdjustedFontSize(90));
//		this._instantRestockingButton.setPosition(TCUtil.getAdjustedX(165), TCUtil.getAdjustedY(230));
//		
//		this._upgradeLabel = GLFactory.getText([TCUtil.getAdjustedX(18), TCUtil.getAdjustedY(290)], 100,"Grade 1", [TCUtil.getAdjustedX(120), TCUtil.getAdjustedY(50)], TCUtil.getAdjustedFontSize(130), null, null, GL2.Text.OverflowMode.ReduceFontSize, [0.0, 0.5], new Core.Color(0,0,0));
//		this._backgroundImage.addChild(this._upgradeLabel);
//				
//		this._upgradeFillerBarHolder = GLFactory.getSprite([TCUtil.getAdjustedX(120),TCUtil.getAdjustedY(260)], null, fillerBarHolderImagePath, [TCUtil.getAdjustedX(170),TCUtil.getAdjustedY(50)]);
//		this._backgroundImage.addChild(this._upgradeFillerBarHolder);				
//		
//		this._upgradeFillerBar = GLFactory.getSprite([TCUtil.getAdjustedX(11),TCUtil.getAdjustedY(4)], null, fillerBarImagePath, [TCUtil.getAdjustedX(100),TCUtil.getAdjustedY(37)]);///x=159 for full
//		this._upgradeFillerBarHolder.addChild(this._upgradeFillerBar);
//		
//		this._upgradingLabel = GLFactory.getText([TCUtil.getAdjustedX(11), TCUtil.getAdjustedY(4)], 100,"upgrading", [TCUtil.getAdjustedX(150), TCUtil.getAdjustedY(40)], TCUtil.getAdjustedFontSize(130), null, null, GL2.Text.OverflowMode.ReduceFontSize, [0.0, 0.0], new Core.Color(0,0,0));
//		this._upgradeFillerBarHolder.addChild(this._upgradingLabel);
//		
//		this._instantUpgradingButton = new GLButton(instantUpgradeImagePathNormal, instantUpgradeImagePathPressed, TCUtil.getAdjustedX(260),TCUtil.getAdjustedY(40), this.onCloseTouch.bind(this), this._backgroundImage, 3, "Instantly finish upgrading now for 3 nuggets!",new Core.Color(0,0,0), null, null, TCUtil.getAdjustedFontSize(90));
//		this._instantUpgradingButton.setPosition(TCUtil.getAdjustedX(165), TCUtil.getAdjustedY(335));		
	},
	
	makeTouchSwallower: function()
    {
    	var width = 0 , height = 0;
    	width = Core.Capabilities.getScreenWidth();
		height = Core.Capabilities.getScreenHeight();
        
    	this._dissolveTouchesTarget = new GL2.TouchTarget();
    	this._dissolveTouchesTarget.setAnchor(0,0);
		this._dissolveTouchesTarget.setSize(TCUtil.getAdjustedX(width),TCUtil.getAdjustedY(height));
        this._dissolveTouchesTarget.setPosition(TCUtil.getAdjustedY(0),TCUtil.getAdjustedX(0));
     
        this._myMessageListener = new Core.MessageListener();
		this._dissolveTouchesTarget.getTouchEmitter().addListener(this._myMessageListener, this.dissolveTouches.bind(this),10); //@ make this 10 go to Constants.
		this.addChild(this._dissolveTouchesTarget);
    },
        
    dissolveTouches: function()
    {
    	return true;
    },
	
	drawRequiredUI: function()
	{
    	if(TutorialManager.getHasCompletedTutorial())
    	{
			this.drawRequiredUpgradeUI();
    	}
    	else 
    	{
    		if(TutorialManager.getCurrentTutorialStep() == 8)
    		{
    			this.drawRequiredUpgradeUI();
    			var tutorialIndicator = this._elevatorContainer._tutorialIndicatorForUpgrade;
    			if(tutorialIndicator)
    			{
    				tutorialIndicator.setPosition([TCUtil.getAdjustedX(240), TCUtil.getAdjustedY(305)]);
    				
					TutorialManager.getTutorialPopup().setCurrentPosition([TCUtil.getAdjustedX(120),TCUtil.getAdjustedY(90)]);
					TutorialManager.getTutorialPopup().updateText(Core.Localization.getString("TUTORIAL_TEXT_8"));
    			}
   			}
   			else if(TutorialManager.getCurrentTutorialStep() == 9)
   			{
   				this.drawRequiredUpgradeUI();
   				var tutorialIndicator = this._elevatorContainer._tutorialIndicatorForInstantUpgrade;
    			if(tutorialIndicator)
    			{
    				tutorialIndicator.setPosition([TCUtil.getAdjustedX(240), TCUtil.getAdjustedY(375)]);
    				TutorialManager.getTutorialPopup().setCurrentPosition([TCUtil.getAdjustedX(120),TCUtil.getAdjustedY(100)]);
    				TutorialManager.getTutorialPopup().updateText(Core.Localization.getString("TUTORIAL_TEXT_9"));
    			}
   			}
    	
    	}
		
	},
	
	drawRequiredUpgradeUI: function()
	{
		this.removeUpgradeUI();

		if(this._redrawUITimeout)
		{
			clearTimeout(this._redrawUITimeout);
			this._redrawUITimeout = null;
		}
		
		this._upgradeLabel = GLFactory.getText([TCUtil.getAdjustedX(55), TCUtil.getAdjustedY(270)], 100, GALocalizationHandler.getRelativeString(Core.Localization.getString("GRADE"),[this._elevatorModel.getUpgradeState() + 1]), [TCUtil.getAdjustedX(100), TCUtil.getAdjustedY(50)], TCUtil.getAdjustedFontSize(120), null, null, GL2.Text.OverflowMode.ReduceFontSize, [0.0, 0.5], new Core.Color(0,0,0));
		this._backgroundImage.addChild(this._upgradeLabel);
		if(this._elevatorModel.canUpgradeElevator())
		{
			if(!this._elevatorModel.isUpgradeInProgress())
			{
				var genericButtonNormalImagePath = this.getBaseResourcePath() + "generic-button-normal.png";
				var genericButtonSelectedImagePath = this.getBaseResourcePath() + "generic-button-pressed.png";
				
				if(this._upgradeButton)
				{
					this._upgradeButton.destroy();
					this._upgradeButton = null;						
				}
						
				this._upgradeButton = new GLButton(genericButtonNormalImagePath, genericButtonSelectedImagePath, TCUtil.getAdjustedX(100),TCUtil.getAdjustedY(40), this.onUpgradeButtonTouch.bind(this), this._backgroundImage, 3, Core.Localization.getString("UPGRADE"),new Core.Color(0,0,0), null, null, TCUtil.getAdjustedFontSize(90));
				this._upgradeButton.setPosition(TCUtil.getAdjustedX(215), TCUtil.getAdjustedY(270));
			}
			else 
			{
				this.createUpgradeUI();
				this._upgradeLabel.setPosition([TCUtil.getAdjustedX(18), TCUtil.getAdjustedY(290)]);
				this._redrawUITimeout = setTimeout(this.drawRequiredUpgradeUI.bind(this), this._elevatorModel.upgradeRemainingTime()*1000);				
			}
		}
		else
		{
			this._upgradeLabel.setPosition([TCUtil.getAdjustedX(110), TCUtil.getAdjustedY(270)]);
		}
	},
	
	createUpgradeUI: function()
	{
		var fillerBarHolderImagePath = this.getBaseResourcePath() + "filler-bar-holder.png";
		var fillerBarImagePath = this.getBaseResourcePath() + "filler-bar.png";

		var instantUpgradeImagePathNormal = this.getBaseResourcePath() + "instant-finish-upgrade-normal.png";
		var instantUpgradeImagePathPressed = this.getBaseResourcePath() + "instant-finish-upgrade-pressed.png";
		
	//	this.removeUpgradeUI();
		
//		this._upgradeLabel = GLFactory.getText([TCUtil.getAdjustedX(18), TCUtil.getAdjustedY(290)], 100,"Grade "+(this._elevatorModel.getUpgradeState() + 1), [TCUtil.getAdjustedX(120), TCUtil.getAdjustedY(50)], TCUtil.getAdjustedFontSize(130), null, null, GL2.Text.OverflowMode.ReduceFontSize, [0.0, 0.5], new Core.Color(0,0,0));
//		this._backgroundImage.addChild(this._upgradeLabel);
//				
		this._upgradeFillerBarHolder = GLFactory.getSprite([TCUtil.getAdjustedX(120),TCUtil.getAdjustedY(260)], null, fillerBarHolderImagePath, [TCUtil.getAdjustedX(170),TCUtil.getAdjustedY(50)]);
		this._backgroundImage.addChild(this._upgradeFillerBarHolder);				
		
		this._upgradeFillerBar = GLFactory.getSprite([TCUtil.getAdjustedX(11),TCUtil.getAdjustedY(4)], null, fillerBarImagePath, [TCUtil.getAdjustedX(0),TCUtil.getAdjustedY(37)]);///x=159 for full
		this._upgradeFillerBarHolder.addChild(this._upgradeFillerBar);
		
		this._upgradingLabel = GLFactory.getText([TCUtil.getAdjustedX(11), TCUtil.getAdjustedY(4)], 100, Core.Localization.getString("UPGRADING"), [TCUtil.getAdjustedX(150), TCUtil.getAdjustedY(40)], TCUtil.getAdjustedFontSize(130), null, null, GL2.Text.OverflowMode.ReduceFontSize, [0.0, 0.0], new Core.Color(0,0,0));
		this._upgradeFillerBarHolder.addChild(this._upgradingLabel);
		
		this._instantUpgradingButton = new GLButton(instantUpgradeImagePathNormal, instantUpgradeImagePathPressed, TCUtil.getAdjustedX(260),TCUtil.getAdjustedY(40), this.onInstantUpgradeTouch.bind(this), this._backgroundImage, 3, Core.Localization.getString("INSTANT_FINISH_UPGRADE"),new Core.Color(0,0,0), null, null, TCUtil.getAdjustedFontSize(90));
		this._instantUpgradingButton.setPosition(TCUtil.getAdjustedX(165), TCUtil.getAdjustedY(335));	
		
		this.refreshUpdateFillerBarFrame();	
	},
	
	removeUpgradeUI: function()
	{
		if(this._upgradeFillerBarUpdateTimeout)
		{
			clearTimeout(this._upgradeFillerBarUpdateTimeout);
			this._upgradeFillerBarUpdateTimeout = null;
		}	
		if(this._upgradeFillerBarHolder)
		{
			this._upgradeFillerBarHolder.destroy();
			this._upgradeFillerBarHolder = null;						
		}
		if(this._upgradeLabel)
		{
			this._upgradeLabel.destroy();
			this._upgradeLabel = null;						
		}
		if(this._upgradeFillerBar)
		{
			this._upgradeFillerBar.destroy();
			this._upgradeFillerBar = null;	
			if(!TutorialManager.getHasCompletedTutorial())
			{
				this.onCloseTouch();
			}					
		}
		if(this._upgradingLabel)
		{
			this._upgradingLabel.destroy();
			this._upgradingLabel = null;						
		}		
		if(this._instantUpgradingButton)
		{
			this._instantUpgradingButton.destroy();
			this._instantUpgradingButton = null;
		}
	},
	
	onUpgradeButtonTouch: function()
	{
		MMSoundManager.playEffect(BUTTON_TAP_SOUND);
		this._elevatorContainer.upgradeElevator();		
		this.destroy();
	},
	
	onInstantUpgradeTouch: function()
	{
		// TODO VALID PURCHASE CHECK :(
		MMSoundManager.playEffect(BUTTON_TAP_SOUND);
		if(this._elevatorContainer._modelObj.canUpgradeBePurchased(kCurrencyType.kCurrencyTypeNuggets,3))
		{
			if(this._elevatorContainer._upgradeTimeout)
			{
				clearTimeout(this._elevatorContainer._upgradeTimeout);
			}
			this._elevatorContainer.upgradeDoneCallBack();
			MMGameState.getCurrentUser().updateCurrency(kOperationType.kOperationSubtractNuggets, 3);
			AnalyticsHandler.logAnalyticsOffer("InstantUpgrade", 3 , "Nuggets",this._elevatorContainer._modelObj.getItemId());
			this.destroy();
			}
		else
		{
			//TODO Put Alert something to show user that he/she dont have enough currency to do action
			var alertBox = UIFactory.getAlertBox(Core.Localization.getString("MESSAGE"), Core.Localization.getString("NOT_ENOUGH_MONEY"), ["ok"], this);
		}
	},
	
	onCloseTouch: function ()
	{
		MMSoundManager.playEffect(BUTTON_TAP_SOUND);
		if(!TutorialManager.getHasCompletedTutorial())
		{
			var tutorialIndicator = this._elevatorContainer._tutorialIndicatorForInstantUpgrade;
			if(tutorialIndicator)
			{
				tutorialIndicator.setPosition([TCUtil.getAdjustedX(30), TCUtil.getAdjustedY(370)]);
				TutorialManager.getTutorialPopup().setCurrentPosition([TCUtil.getAdjustedX(0),TCUtil.getAdjustedY(160)]);
				TutorialManager.getTutorialPopup().updateText(Core.Localization.getString("TUTORIAL_TEXT_9"));	
			}
		}
		this.destroy();
	},
	
	refreshUpdateFillerBarFrame: function()
	{
		var fillerBarImagePath = this.getBaseResourcePath() + "filler-bar.png";
		var upgradeFillerBarFrameRatio = this._elevatorModel.getUpgradeProgressPercentage()/100;
		this._upgradeFillerBar.setImage(fillerBarImagePath, [TCUtil.getAdjustedX(159*upgradeFillerBarFrameRatio),TCUtil.getAdjustedY(37)], [0, 0]);
		this._upgradeFillerBarUpdateTimeout = setTimeout(this.refreshUpdateFillerBarFrame.bind(this), 1000);
	},
	
		
	getBaseResourcePath: function()
	{
		return "./Content/images/gamelayer/controllayer/attributewindow/";
	},
	
	destroy: function ()
	{
		if(this._upgradeFillerBarUpdateTimeout)
		{
			clearTimeout(this._upgradeFillerBarUpdateTimeout);
			this._upgradeFillerBarUpdateTimeout = null;
		}
		if(this._redrawUITimeout)
		{
			clearTimeout(this._redrawUITimeout);
			this._redrawUITimeout = null;
		}
		if(this._backgroundImage)
		{
			this._backgroundImage.destroy();
			this._backgroundImage = null;
		}
		if(this._closeButton)
		{
			this._closeButton.destroy();
			this._closeButton = null;
		}
		if(this._elevatorName)
		{
			this._elevatorName.destroy();
			this._elevatorName = null;
		}
		if(this._dissolveTouchesTarget)
		{
			this._dissolveTouchesTarget.destroy();
			this._dissolveTouchesTarget = null;
		}
		if(this._myMessageListener)
		{
			this._myMessageListener.destroy();
			this._myMessageListener = null;
		}
		if(this._upgradeButton)
		{
			this._upgradeButton.destroy();
			this._upgradeButton = null;						
		}
		if(this._upgradeFillerBarHolder)
		{
			this._upgradeFillerBarHolder.destroy();
			this._upgradeFillerBarHolder = null;						
		}
		if(this._upgradeLabel)
		{
			this._upgradeLabel.destroy();
			this._upgradeLabel = null;						
		}
		if(this._upgradeFillerBar)
		{
			this._upgradeFillerBar.destroy();
			this._upgradeFillerBar = null;						
		}
		if(this._upgradingLabel)
		{
			this._upgradingLabel.destroy();
			this._upgradingLabel = null;						
		}		
		if(this._instantUpgradingButton)
		{
			this._instantUpgradingButton.destroy();
			this._instantUpgradingButton = null;
		}	
		if(this._removeElevatorButton)
		{
			this._removeElevatorButton.destroy();
			this._removeElevatorButton = null;
		}
		if(this._thumbImage)
		{
			this._thumbImage.destroy();
			this._thumbImage = null;
		}
		if(this._elevatorDescription)
		{
			this._elevatorDescription.destroy();
			this._elevatorDescription = null;
		}
			
	}
});